﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Ftest.Jump
{
    public partial class frmMain : Form
    {
        private readonly int MOUSEEVENTF_LEFTDOWN = 0x0002;//模拟鼠标移动
        private readonly int MOUSEEVENTF_MOVE = 0x0001;//模拟鼠标左键按下
        private readonly int MOUSEEVENTF_LEFTUP = 0x0004;//模拟鼠标左键抬起
        private readonly int MOUSEEVENTF_ABSOLUTE = 0x8000;//鼠标绝对位置
        //private readonly int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下 
        //private readonly int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起 
        //private readonly int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //模拟鼠标中键按下 
        //private readonly int MOUSEEVENTF_MIDDLEUP = 0x0040;// 模拟鼠标中键抬起 
        [DllImport("user32")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

        int nScreenWidth, nScreenHeight;//主屏幕分辨率（宽、高）
        //int x ,y;
        int timmer;
        double dbDistance = 0;//两点间距离
        //int downX = 0, downY = 0, upX = 0, upY = 0;//两点坐标
        Point pDown, pUp;//两点
        bool bMouseIsDown = false;
        bool bMouseIsDownGetFocus = false;
        Point pMouesCur;//鼠标当前坐标（程序界面中坐标）
        Point pMobil;//手机点击位置坐标

        #region 通用方法
        private Image GetPrtSc()
        {
            int iWidth = Screen.PrimaryScreen.Bounds.Width;
            //屏幕高
            int iHeight = Screen.PrimaryScreen.Bounds.Height;
            //按照屏幕宽高创建位图
            Image img = new Bitmap(iWidth, iHeight);
            //从一个继承自Image类的对象中创建Graphics对象
            Graphics gc = Graphics.FromImage(img);
            //抓屏并拷贝到myimage里
            gc.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
            return img;
        }

        private double GetDistance(Point p1, Point p2)
        {
            return Math.Sqrt(Math.Pow(Math.Max(p1.X, p2.X) - Math.Min(p1.X, p2.X), 2) + Math.Pow(Math.Max(p1.Y, p2.Y) - Math.Min(p1.Y, p2.Y), 2));
        }
        #endregion

        public frmMain()
        {
            InitializeComponent();
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            nScreenWidth = Screen.PrimaryScreen.Bounds.Width;
            nScreenHeight = Screen.PrimaryScreen.Bounds.Height;
            //x = 1 * 65535 / nScreenWidth;
            //y = 1 * 65535 / nScreenHeight;
            pMobil = new Point(0, 0);
            //pannel显示设置
            pForMobilClickPoint.Dock = DockStyle.Fill;
        }
        private void btJump_Click(object sender, EventArgs e)
        {
            startJump();
        }
        private void btPrtSc_Click(object sender, EventArgs e)
        {
            this.BackgroundImage = GetPrtSc();
        }

        #region Jump
        private  void startJump()
        {
            mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, pMobil.X, pMobil.Y, 0, 0);//移动到需要点击的位置
            mouse_event(MOUSEEVENTF_LEFTDOWN, pMobil.X, pMobil.Y, 0, 0);//点击
            timmer = 0;
            timer1.Start();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            bool nIsCompleted = false;
            timmer += timer1.Interval;
            //label3.Text = timmer.ToString() + "/" + ((int)(jl / (double)numericUpDown1.Value)).ToString();            
            while (timmer > (int)(dbDistance/(double)edtPerDistance.Value))
            {
                mouse_event(MOUSEEVENTF_LEFTUP, pMobil.X, pMobil.Y, 0, 0);//抬起
                timer1.Stop();
                nIsCompleted = true;
                break;
            }
            //
            if (nIsCompleted)
            {
                //暂停一下，以便获取小球跳完的界面
                System.Threading.Thread.Sleep(1500);                
                //获取当前界面
                this.BackgroundImage = GetPrtSc();
                //恢复鼠标位置
                mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, (pMouesCur.X+this.Left) * 65535 / nScreenWidth, (pMouesCur.Y+this.Top) * 65535 / nScreenHeight, 0, 0);//移动到需要点击的位置
            }
        }
        #endregion
        
        private void frmMain_MouseDown(object sender, MouseEventArgs e)
        {
            //记录鼠标当前所在位置(程序界面中的位置)
            pMouesCur = new Point(e.X, e.Y);
            pDown = pMouesCur;
            //记录鼠标当前所在位置(屏幕中的位置)
            //pDown = new Point(MousePosition.X, MousePosition.Y);
            //downX = MousePosition.X;
            //downY = MousePosition.Y;
            //Console.WriteLine(e.X.ToString() + "|||" + downX.ToString());
            //this.Text = "X:" + downX + "Y:" + downY;
            bMouseIsDown = true;
            //p1 = pMouesCur;
        }

        private void frmMain_MouseUp(object sender, MouseEventArgs e)
        {
            bMouseIsDown = false;
            //记录鼠标当前所在位置(程序界面中的位置)
            pUp = new Point(e.X, e.Y);
            //记录鼠标当前所在位置(屏幕中的位置)
            //upX = MousePosition.X;
            //upY = MousePosition.Y;
            //this.Text += " | X:" + upX + "Y:" + upY;
            //获取两点距离
            dbDistance = GetDistance(pUp, pDown) * 1000;
            //
           edtMsg.Text = "距离："+dbDistance.ToString() + " | 按压时长：" + (int)(dbDistance / (double)edtPerDistance.Value);
            //开始跳
            startJump();
        }
        private void frmMain_MouseMove(object sender, MouseEventArgs e)
        {
            if (bMouseIsDown)
            {//画参照线
                using (Graphics g = this.CreateGraphics())
                {
                    //g.Clear(this.BackColor);
                    //g.Clear(Color.Transparent);
                    if(this.BackgroundImage==null)//如果窗体没有设置截屏图像，则显示背景颜色
                        g.Clear(this.BackColor);
                    else
                        g.DrawImage(this.BackgroundImage, default(Point));
                    //g.DrawLine(Pens.Red, pDown, new Point(e.X, e.Y));
                    g.DrawLine(new Pen(Color.Red,2), pDown, new Point(e.X, e.Y));
                }
            }
        }       

        private void SetMobilClickPointBeg()
        {           
            pbForMobilClickPoint.Image = GetPrtSc();
            //主界面显示设置
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            pForMobilClickPoint.Visible = true;

            bMouseIsDownGetFocus = true;

            //label2.Location = new Point(e.X, e.Y);
            //label2.Text = "+ 当前坐标：" + e.X + "," + e.Y;
        }

        private void SetMobilClickPointEnd()
        {
            bMouseIsDownGetFocus = false;
            //主界面显示设置
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
            this.WindowState = FormWindowState.Normal;
            pForMobilClickPoint.Visible = false;
            //pMobilPhone =new Point(e.X, e.Y);
            //坐标需要换算一下
            pMobil.X = MousePosition.X * 65535 / nScreenWidth;
            pMobil.Y = MousePosition.Y * 65535 / nScreenHeight;
        }

        private void pbForMobilClickPoint_MouseMove(object sender, MouseEventArgs e)
        {
            if (bMouseIsDownGetFocus)
            {
                lbForMobilClickPoint.Location = new Point(e.X+10 , e.Y+10);
                lbForMobilClickPoint.Text = "当前坐标：" + e.X + "," + e.Y+"\r\n单击完成选择";
            }
        }


        private void btSetMobilClickPoint_Click(object sender, EventArgs e)
        {
            SetMobilClickPointBeg();
        }
        private void pbForMobilClickPoint_MouseClick(object sender, MouseEventArgs e)
        {
            SetMobilClickPointEnd();
        }

        private void label2_Click(object sender, EventArgs e)
        {
            SetMobilClickPointEnd();
        }




    }
}
